package com.icode.web.startup.security;

import com.icode.core.dto.UserDTO;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * Created by ZhongGang
 * at 2015/1/24
 */
public class SecurityUtil {

    public static UserDTO currentUser() {
        SecurityContext context = SecurityContextHolder.getContext();
        if (context == null) {
            throw new UnsupportedOperationException("no user logon into system, please login first!");
        }

        Authentication authentication = context.getAuthentication();
        if (authentication == null) {
            throw new UnsupportedOperationException("no user logon into system, please login first!");
        }

        return (UserDTO) authentication.getPrincipal();
    }
}
